package com.zhangxin.aiInterview.manager.redis.audio_speed;

import com.zhangxin.aiInterview.manager.redis.audio_speed.model.AudioSpeedEntry;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 获取用户的语速和文本内容
 */
@Component
public class GetAudioSpeeds {
    
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 获取用户的所有语速记录
     * @param userId 用户ID
     * @return 语速记录列表
     */
    public List<AudioSpeedEntry> getAudioSpeeds(Long userId) {
        String key = "AI_interview:audioSpeed:" + userId;
        
        try {
            // 获取 Hash 中的所有键值对
            Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(key);
            
            // 转换为 Java 对象列表
            List<AudioSpeedEntry> result = new ArrayList<>();
            for (Map.Entry<Object, Object> entry : entries.entrySet()) {
                result.add(new AudioSpeedEntry(
                    (String) entry.getValue(),  // text
                (String) entry.getKey()   // speed
                ));
            }
            return result;
        } catch (Exception e) {
            System.out.println("Redis 获取语速 Hash 数据失败: " + e.getMessage());
            return new ArrayList<>();  // 返回空列表而不是null
        }
    }
}